## title:   Narcissism in Political Participation
## authors: Z. Fazekas & P.K. Hatemi
## goal:    display results from regression models, coef plots
## ----
## 0. packages and functions --
library("dplyr")
library("reshape2")
library("ggplot2")
library("gridExtra")

## 1. load data -- 
source("helper.R")
model_results <- read.csv("./data/model-coef-all.csv", 
                          stringsAsFactors = FALSE)
model_results$is_sig <- 0
model_results$is_sig[model_results$p.value < 0.05] <- 1
head(model_results)

model_results <- dplyr::select(model_results, study:std.error, outcome_str, 
                               term_str, is_sig, narc_op, spec, adj_r2, model, n)
model_results$facet_id <- model_results$outcome_str 

turnout_results <- read.csv("./data/turnout-coef-all.csv")

turnout_results$is_sig <- 0
turnout_results$is_sig[turnout_results$p.value < 0.05] <- 1

turnout_results <- dplyr::select(turnout_results, study, outcome, 
                                 term, estimate, std.error, outcome_str, term_str, is_sig,
                                 narc_op, spec)
turnout_results$model <- "NAP"
turnout_results$n <- "NAP"
turnout_results$adj_r2 <- "NAP"
turnout_results$facet_id <- "Turnout"
model_results_all <- rbind(model_results, turnout_results)


## 3-study plotting
model_results <- filter(model_results_all, narc_op == "Full")
model_results$facet_id <- factor(model_results$facet_id, 
                                levels = c("Participation",
                                           "Turnout"))
model_results$term_str <- factor(model_results$term_str, 
                                levels = c("Exhibitionism",
                                           "Vanity",
                                           "Self-sufficiency",
                                           "Entitlement",
                                           "Exploitativeness",
                                           "Superiority",
                                           "Authority",
                                           "Narcissism"))

## drop terms not of interest for main text plots
narc_results <- filter(model_results, term != "age_sd" & term != "edu_cat" &
                       term != "(Intercept)" & term != "not_caucasian" &
                       term != "female")

narc_results$study <- factor(narc_results$study, 
                                 levels = c("US15",
                                            "US13",
                                            "DK11"))

## 3. full plot
narc_results$turn_id <- 0
narc_results$turn_id[narc_results$outcome_str == "Turnout (2014 midterm)"] <- 1

narc_results_npi <- filter(narc_results, term == "npi_sd")

edu_effect <- mean(model_results[model_results$term == "edu_cat" & model_results$spec == "socdem" &
                model_results$model == "summed_scale", "estimate"])
edu_turn_effect <- 0.8397892

labs <- model_results[model_results$term == "edu_cat" & model_results$spec == "socdem" &
                model_results$model == "summed_scale", c("study", "adj_r2", "n")]
labs$estimate <- -0.05
labs$lab <- paste0(labs$adj_r2, "\n(", labs$n, ")")
labs$facet_id <- "Participation"

narc_results_npi$text <- ""
narc_results_npi$text[narc_results_npi$turn_id == 1] <- "2014 Midterm Elections"

narc_results_npi$study <- factor(narc_results_npi$study, 
                                 levels = c("US15",
                                            "US13",
                                            "DK11"))
labs$study <- factor(labs$study, 
                     levels = c("US15",
                                "US13",
                                "DK11"))

labs_2 <- data.frame(estimate = edu_effect + 0.005,
                     study = "DK11",
                     facet_id = "Participation")
labs_3 <- data.frame(estimate = edu_turn_effect - 0.005,
                     study = "US13",
                     facet_id = "Turnout")



ggplot(filter(narc_results_npi, spec == "socdem"),
       aes(x = study, y = estimate,
           ymin  = estimate - 1.96*std.error,
           ymax  = estimate + 1.96*std.error,
           alpha = as.factor(is_sig),
           group  = factor(turn_id), 
           shape = factor(turn_id),
           label = round(estimate, 3))) +
  geom_hline(yintercept = 0, linetype = 1, alpha = 0.2) +
  geom_hline(data = filter(narc_results_npi, spec == "socdem" & 
                           facet_id == "Participation"), aes(yintercept = edu_effect), linetype = 2, alpha = 0.2) +
  geom_hline(data = filter(narc_results_npi, spec == "socdem" & 
                             facet_id == "Turnout"), aes(yintercept = edu_turn_effect), linetype = 2, alpha = 0.2) +
  geom_point(size = 4, position = position_dodge(width = 0.6)) +
  geom_linerange(size = 1.25, position = position_dodge(width = 0.6)) + 
  geom_text(position = position_dodge(width = 0.6), vjust = -1) +
  geom_text(data = labs, aes(x = study, y = estimate, label = lab),
            inherit.aes = FALSE, hjust = 0) +
  geom_text(data = labs_2, aes(x = study, y = estimate, label = "Effect of\nhigher education"),
            inherit.aes = FALSE, hjust = 0, vjust = -1) +
  geom_text(data = labs_3, aes(x = study, y = estimate, label = "Effect of\nhigher education"),
            inherit.aes = FALSE, hjust = 1, vjust = -1.25) +
  scale_alpha_manual("", values = c("0" = "0.25",
                                    "1" = "1"),
                     guide = FALSE) +
  scale_shape_discrete(guide = FALSE) +
  facet_wrap(~facet_id, scales = "free_x", ncol = 2) +
  coord_flip() +
  xlab("") + ylab("") +
  theme_minimal(base_size = 14) +
  theme(panel.background = element_rect(fill = "grey98", colour = NA)) +
  theme(strip.text = element_text(size = 16),
        axis.text.y = element_text(size = 14),
        axis.text.x = element_text(size = 12),
        panel.grid.major.y = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.grid.major.x = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.spacing = unit(1.5, "lines"),
        axis.title.x = element_text(size  = 10, hjust = 1, vjust = 0),
        plot.margin = unit(c(1,1,1,1), "cm")) +
  labs(caption = "Note: Lines are 95% confidence intervals. Coefficient plot (2 SD difference in narcissism).
                  Continuous outcome on [0, 1] range for participation, dichotomous outcome for turnout. Triangle for 2014 Midterm vote.")


narc_results$study <- factor(narc_results$study, 
                             levels = c("DK11", "US13",
                                        "US15"))
narc_results <- na.omit(narc_results)

labs <- model_results[model_results$term == "edu_cat" & model_results$spec == "socdem" &
                        model_results$model == "sub_facets", c("study", "adj_r2", "n")]
labs$estimate <- 0.11
labs$lab <- paste0(labs$adj_r2, "\n(", labs$n, ")")
labs$facet_id <- "Participation"
labs$term_str <- "Entitlement"


ggplot(filter(narc_results, term_str != "Narcissism" & spec == "socdem"), 
       aes(x = term_str, y = estimate,
           ymin  = estimate - 1.96*std.error,
           ymax  = estimate + 1.96*std.error,
           alpha = as.factor(is_sig),
           group  = factor(turn_id), 
           shape = factor(turn_id),
           label = round(estimate, 3))) +
  geom_hline(yintercept = 0, linetype = 1, alpha = 0.2) +
  geom_point(size = 2, position = position_dodge(width = 0.8)) +
  geom_linerange(size = 1, position = position_dodge(width = 0.8)) + 
  scale_alpha_manual("", values = c("0" = "0.25",
                                    "1" = "1"),
                     guide = FALSE) +
  facet_grid(study~facet_id, scales = "free_x") +
  coord_flip() +
  geom_text(data = labs, aes(x = term_str, y = estimate, label = lab),
            inherit.aes = FALSE, hjust = 0) +
  geom_text(size = 2.5, position = position_dodge(width = 0.8), vjust = -0.75) +
  scale_shape_discrete(guide = FALSE) +
  xlab("") + ylab("") +
  theme_minimal(base_size = 14) +
  theme(panel.background = element_rect(fill = "grey98", colour = NA)) +
  theme(strip.text = element_text(size = 16),
        axis.text.y = element_text(size = 14),
        axis.text.x = element_text(size = 12),
        panel.grid.major.y = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.grid.major.x = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.spacing = unit(1.5, "lines"),
        axis.title.x = element_text(size  = 10, hjust = 1, vjust = 0),
        plot.margin = unit(c(1,1,1,1), "cm"),
        strip.text.y = element_text(angle = 360)) +
  labs(caption = "Note: Lines are 95% confidence intervals. Coefficient plot (2 SD difference in narcissism).
       Continuous outcome on [0, 1] range for participation, dichotomous outcome for turnout. Triangle for 2014 Midterm vote.")

model_results <- filter(model_results_all, narc_op == "Three")
model_results$facet_id <- factor(model_results$facet_id, 
                                 levels = c("Participation",
                                            "Turnout"))
model_results$term_str[model_results$term_str == "Grandiose/Exhibitionism"] <- "Grandiose Exhibitionism"
model_results$term_str <- factor(model_results$term_str, 
                                 levels = c("Entitlement/Exploitativeness",
                                            "Grandiose Exhibitionism",
                                            "Leadership/Authority"))

## drop terms not of interest for main text plots
narc_results <- filter(model_results, term != "age_sd" & term != "edu_cat" &
                         term != "(Intercept)" & term != "not_caucasian" &
                         term != "female")

narc_results$study <- factor(narc_results$study, 
                             levels = c("US15",
                                        "US13",
                                        "DK11"))

narc_results$turn_id <- 0
narc_results$turn_id[narc_results$outcome_str == "Turnout (2014 midterm)"] <- 1

narc_results$study <- factor(narc_results$study, 
                             levels = c("DK11", "US13",
                                        "US15"))

narc_results <- na.omit(narc_results)

labs <- model_results[model_results$term == "edu_cat" & model_results$spec == "socdem" &
                        model_results$model == "sub_facets", c("study", "adj_r2", "n")]
labs$estimate <- 0.11
labs$lab <- paste0(labs$adj_r2, "\n(", labs$n, ")")
labs$facet_id <- "Participation"
labs$term_str <- "Grandiose Exhibitionism"

ggplot(filter(narc_results, term_str != "Narcissism" & spec == "socdem"), 
       aes(x = term_str, y = estimate,
           ymin  = estimate - 1.96*std.error,
           ymax  = estimate + 1.96*std.error,
           alpha = as.factor(is_sig),
           group  = factor(turn_id), 
           shape = factor(turn_id),
           label = round(estimate, 3))) +
  geom_hline(yintercept = 0, linetype = 1, alpha = 0.2) +
  geom_point(size = 2, position = position_dodge(width = 0.7)) +
  geom_linerange(size = 1, position = position_dodge(width = 0.7)) + 
  scale_alpha_manual("", values = c("0" = "0.25",
                                    "1" = "1"),
                     guide = FALSE) +
  facet_grid(study~facet_id, scales = "free_x") +
  geom_text(data = labs, aes(x = term_str, y = estimate, label = lab),
            inherit.aes = FALSE, size = 3, hjust = 0) +
  geom_text(size = 2.5, position = position_dodge(width = 0.8), vjust = -0.75) +
  coord_flip() +
  scale_shape_discrete(guide = FALSE) +
  xlab("") + ylab("") +
  theme_minimal(base_size = 14) +
  theme(panel.background = element_rect(fill = "grey98", colour = NA)) +
  theme(strip.text = element_text(size = 16),
        axis.text.y = element_text(size = 14),
        axis.text.x = element_text(size = 12),
        panel.grid.major.y = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.grid.major.x = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.spacing = unit(1.5, "lines"),
        axis.title.x = element_text(size  = 10, hjust = 1, vjust = 0),
        plot.margin = unit(c(1,1,1,1), "cm"),
        strip.text.y = element_text(angle = 360)) +
  labs(caption = "Note: Lines are 95% confidence intervals. Coefficient plot (2 SD difference in narcissism).
       Continuous outcome on [0, 1] range for participation, dichotomous outcome for turnout. Triangle for 2014 Midterm vote.")


## Same plots, interest control
model_results <- filter(model_results_all, narc_op == "Full")
model_results$facet_id <- factor(model_results$facet_id, 
                                 levels = c("Participation",
                                            "Turnout"))
model_results$term_str <- factor(model_results$term_str, 
                                 levels = c("Exhibitionism",
                                            "Vanity",
                                            "Self-sufficiency",
                                            "Entitlement",
                                            "Exploitativeness",
                                            "Superiority",
                                            "Authority",
                                            "Narcissism"))

## drop terms not of interest for main text plots
narc_results <- filter(model_results, term != "age_sd" & term != "edu_cat" &
                         term != "(Intercept)" & term != "not_caucasian" &
                         term != "female")

narc_results$study <- factor(narc_results$study, 
                             levels = c("US15",
                                        "US13",
                                        "DK11"))

narc_results$turn_id <- 0
narc_results$turn_id[narc_results$outcome_str == "Turnout (2014 midterm)"] <- 1

narc_results_npi <- filter(narc_results, term == "npi_sd")

int_effect <- mean(model_results[model_results$term == "interest_sd" & model_results$spec == "all" &
                                   model_results$model == "summed_scale", "estimate"])
int_turn_effect <- 0.9098264

labs <- model_results[model_results$term == "edu_cat" & model_results$spec == "all" &
                        model_results$model == "summed_scale", c("study", "adj_r2", "n")]
labs$estimate <- -0.05
labs$lab <- paste0(labs$adj_r2, "\n(", labs$n, ")")
labs$facet_id <- "Participation"

narc_results_npi$text <- ""
narc_results_npi$text[narc_results_npi$turn_id == 1] <- "2014 Midterm Elections"

narc_results_npi$study <- factor(narc_results_npi$study, 
                                 levels = c("US15",
                                            "US13",
                                            "DK11"))
labs$study <- factor(labs$study, 
                     levels = c("US15",
                                "US13",
                                "DK11"))

labs_2 <- data.frame(estimate = int_effect + 0.005,
                     study = "DK11",
                     facet_id = "Participation")
labs_3 <- data.frame(estimate = int_turn_effect - 0.005,
                     study = "US13",
                     facet_id = "Turnout")

ggplot(filter(narc_results_npi, spec == "all"),
       aes(x = study, y = estimate,
           ymin  = estimate - 1.96*std.error,
           ymax  = estimate + 1.96*std.error,
           alpha = as.factor(is_sig),
           group  = factor(turn_id), 
           shape = factor(turn_id),
           label = round(estimate, 3))) +
  geom_hline(yintercept = 0, linetype = 1, alpha = 0.2) +
  geom_hline(data = filter(narc_results_npi, spec == "socdem" & 
                             facet_id == "Participation"), aes(yintercept = int_effect), linetype = 2, alpha = 0.2) +
  geom_hline(data = filter(narc_results_npi, spec == "socdem" & 
                             facet_id == "Turnout"), aes(yintercept = int_turn_effect), linetype = 2, alpha = 0.2) +
  geom_point(size = 4, position = position_dodge(width = 0.6)) +
  geom_linerange(size = 1.25, position = position_dodge(width = 0.6)) + 
  geom_text(position = position_dodge(width = 0.6), vjust = -1) +
  geom_text(data = labs, aes(x = study, y = estimate, label = lab),
            inherit.aes = FALSE, hjust = 0) +
  geom_text(data = labs_2, aes(x = study, y = estimate, label = "Effect of\ninterest"),
            inherit.aes = FALSE, hjust = 1, vjust = -1) +
  geom_text(data = labs_3, aes(x = study, y = estimate, label = "Effect of\ninterest"),
            inherit.aes = FALSE, hjust = 1, vjust = -1.25) +
  scale_alpha_manual("", values = c("0" = "0.25",
                                    "1" = "1"),
                     guide = FALSE) +
  scale_shape_discrete(guide = FALSE) +
  facet_wrap(~facet_id, scales = "free_x", ncol = 2) +
  coord_flip() +
  xlab("") + ylab("") +
  theme_minimal(base_size = 14) +
  theme(panel.background = element_rect(fill = "grey98", colour = NA)) +
  theme(strip.text = element_text(size = 16),
        axis.text.y = element_text(size = 14),
        axis.text.x = element_text(size = 12),
        panel.grid.major.y = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.grid.major.x = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.spacing = unit(1.5, "lines"),
        axis.title.x = element_text(size  = 10, hjust = 1, vjust = 0),
        plot.margin = unit(c(1,1,1,1), "cm")) +
  labs(caption = "Note: Lines are 95% confidence intervals. Coefficient plot (2 SD difference in narcissism).
       Continuous outcome on [0, 1] range for participation, dichotomous outcome for turnout. Triangle for 2014 Midterm vote.
       Political interest control included.")


narc_results$study <- factor(narc_results$study, 
                             levels = c("DK11", "US13",
                                        "US15"))

narc_results <- na.omit(narc_results)

labs <- model_results[model_results$term == "edu_cat" & model_results$spec == "all" &
                        model_results$model == "sub_facets", c("study", "adj_r2", "n")]
labs$estimate <- 0.075
labs$lab <- paste0(labs$adj_r2, "\n(", labs$n, ")")
labs$facet_id <- "Participation"
labs$term_str <- "Entitlement"

ggplot(filter(narc_results, term_str != "Narcissism" & spec == "all"), 
       aes(x = term_str, y = estimate,
           ymin  = estimate - 1.96*std.error,
           ymax  = estimate + 1.96*std.error,
           alpha = as.factor(is_sig),
           group  = factor(turn_id), 
           shape = factor(turn_id),
           label = round(estimate, 3))) +
  geom_hline(yintercept = 0, linetype = 1, alpha = 0.2) +
  geom_point(size = 2.5, position = position_dodge(width = 0.7)) +
  geom_linerange(size = 1, position = position_dodge(width = 0.7)) + 
  scale_alpha_manual("", values = c("0" = "0.25",
                                    "1" = "1"),
                     guide = FALSE) +
  geom_text(data = labs, aes(x = term_str, y = estimate, label = lab),
            inherit.aes = FALSE, hjust = 0) +
  geom_text(size = 2.5, position = position_dodge(width = 0.8), vjust = -0.75) +
  facet_grid(study~facet_id, scales = "free_x") +
  coord_flip() +
  scale_shape_discrete(guide = FALSE) +
  xlab("") + ylab("") +
  theme_minimal(base_size = 14) +
  theme(panel.background = element_rect(fill = "grey98", colour = NA)) +
  theme(strip.text = element_text(size = 16),
        axis.text.y = element_text(size = 14),
        axis.text.x = element_text(size = 12),
        panel.grid.major.y = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.grid.major.x = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.spacing = unit(1.5, "lines"),
        axis.title.x = element_text(size  = 10, hjust = 1, vjust = 0),
        plot.margin = unit(c(1,1,1,1), "cm"),
        strip.text.y = element_text(angle = 360)) +
  labs(caption = "Note: Lines are 95% confidence intervals. Coefficient plot (2 SD difference in narcissism).
       Continuous outcome on [0, 1] range for participation, dichotomous outcome for turnout. Triangle for 2014 Midterm vote.
       Political interest control included.")


model_results <- filter(model_results_all, narc_op == "Three")
model_results$facet_id <- factor(model_results$facet_id, 
                                 levels = c("Participation",
                                            "Turnout"))
model_results$term_str[model_results$term_str == "Grandiose/Exhibitionism"] <- "Grandiose Exhibitionism"
model_results$term_str <- factor(model_results$term_str, 
                                 levels = c("Entitlement/Exploitativeness",
                                            "Grandiose Exhibitionism",
                                            "Leadership/Authority"))

## drop terms not of interest for main text plots
narc_results <- filter(model_results, term != "age_sd" & term != "edu_cat" &
                         term != "(Intercept)" & term != "not_caucasian" &
                         term != "female")

narc_results$study <- factor(narc_results$study, 
                             levels = c("US15",
                                        "US13",
                                        "DK11"))

narc_results$turn_id <- 0
narc_results$turn_id[narc_results$outcome_str == "Turnout (2014 midterm)"] <- 1

narc_results$study <- factor(narc_results$study, 
                             levels = c("DK11", "US13",
                                        "US15"))

narc_results <- na.omit(narc_results)

labs <- model_results[model_results$term == "edu_cat" & model_results$spec == "all" &
                        model_results$model == "sub_facets", c("study", "adj_r2", "n")]
labs$estimate <- 0.075
labs$lab <- paste0(labs$adj_r2, "\n(", labs$n, ")")
labs$facet_id <- "Participation"
labs$term_str <- "Entitlement/Exploitativeness"

ggplot(filter(narc_results, term_str != "Narcissism" & spec == "all"), 
       aes(x = term_str, y = estimate,
           ymin  = estimate - 1.96*std.error,
           ymax  = estimate + 1.96*std.error,
           alpha = as.factor(is_sig),
           group  = factor(turn_id), 
           shape = factor(turn_id),
           label = round(estimate, 3))) +
  geom_hline(yintercept = 0, linetype = 1, alpha = 0.2) +
  geom_point(size = 2.5, position = position_dodge(width = 0.7)) +
  geom_linerange(size = 1, position = position_dodge(width = 0.7)) + 
  scale_alpha_manual("", values = c("0" = "0.25",
                                    "1" = "1"),
                     guide = FALSE) +
  geom_text(data = labs, aes(x = term_str, y = estimate, label = lab),
            inherit.aes = FALSE, size = 3, hjust = 0) +
  geom_text(size = 2.5, position = position_dodge(width = 0.8), vjust = -0.75) +
  facet_grid(study~facet_id, scales = "free_x") +
  coord_flip() +
  scale_shape_discrete(guide = FALSE) +
  xlab("") + ylab("") +
  theme_minimal(base_size = 14) +
  theme(panel.background = element_rect(fill = "grey98", colour = NA)) +
  theme(strip.text = element_text(size = 16),
        axis.text.y = element_text(size = 14),
        axis.text.x = element_text(size = 12),
        panel.grid.major.y = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.grid.major.x = element_line(colour = "grey90", size = 0.2, linetype = 2),
        panel.spacing = unit(1.5, "lines"),
        axis.title.x = element_text(size  = 10, hjust = 1, vjust = 0),
        plot.margin = unit(c(1,1,1,1), "cm"),
        strip.text.y = element_text(angle = 360)) +
  labs(caption = "Note: Lines are 95% confidence intervals. Coefficient plot (2 SD difference in narcissism).
       Continuous outcome on [0, 1] range for participation, dichotomous outcome for turnout. Triangle for 2014 Midterm vote.
       Political interest control included.")

# R version 3.5.1 (2018-07-02)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS  10.15.4
# 
# Matrix products: default
# BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] gridExtra_2.3  ggplot2_3.2.1  reshape2_1.4.3 dplyr_0.8.3   
# 
# loaded via a namespace (and not attached):
#   [1] Rcpp_1.0.2       rstudioapi_0.10  magrittr_1.5     tidyselect_0.2.5 munsell_0.5.0    colorspace_1.4-1 R6_2.4.0        
# [8] rlang_0.4.2      stringr_1.4.0    plyr_1.8.4       tools_3.5.1      grid_3.5.1       gtable_0.3.0     withr_2.1.2     
# [15] yaml_2.2.0       lazyeval_0.2.2   assertthat_0.2.1 tibble_2.1.3     crayon_1.3.4     purrr_0.3.3      glue_1.3.1      
# [22] labeling_0.3     stringi_1.4.3    compiler_3.5.1   pillar_1.4.2     scales_1.0.0     pkgconfig_2.0.3 
